import { Effect } from 'dva';
import { Reducer } from 'redux';
import { selectMenuListByUser } from '@/services/login';
import { message } from 'antd';
// id:0, menuName:'首页',routerUrl:'/index',menuIcon:'fa-book',children:null,affix:true,hidden:true

export interface MenuItem {
  id: Number;
  menuName: string;
  routerUrl: string;
  menuIcon: string;
  children?: MenuItem[];
  affix: string;
  hidden: string;
}

export interface MenuModelState {
  menuList: MenuItem[];
}

export interface UserModelType {
  namespace: string;
  state: MenuModelState;
  effects: {
    fetchMenu: Effect;
  };
  reducers: {
    setMenu: Reducer<MenuModelState>;
  };
}

const MenuModel: UserModelType = {
  namespace: 'menus',
  state: {
    menuList: [],
  },
  effects: {
    *fetchMenu(_, { call, put }) {
      const res = yield call(selectMenuListByUser);
      let menus: MenuItem[] = [];
      if (res.code === 0) {
        menus = res.data;
      } else {
        // message.error(res.msg);
      }

      yield put({
        type: 'setMenu',
        payload: menus,
      });
    },
  },

  reducers: {
    setMenu(data = { menuList: [] }, { payload }) {
      return {
        ...data,
        menuList: payload,
      };
    },

  },
};

export default MenuModel;
